package com.haiheng.library_base.utils

import android.content.Context
import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.integration.webp.decoder.WebpDrawable
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target

/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2023/11/27 0027
 * @Version: 1.0
 */
object GlideUtils {
    fun loadWebpGif(context:Context,iv: ImageView, anim: Int?, defaultIcon: Int) {
        Glide.with(context)
            .load(anim)
            .addListener(object : RequestListener<Drawable?> {
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: com.bumptech.glide.request.target.Target<Drawable?>?,
                    isFirstResource: Boolean
                ): Boolean {
                    iv.setImageResource(defaultIcon)
                    return false
                }

                override fun onResourceReady(
                    resource: Drawable?,
                    model: Any?,
                    target: Target<Drawable?>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    if (resource is WebpDrawable) {
                        //循环
                        resource.loopCount = -1
                    }
                    return false
                }
            })
            .into(iv)
    }

}